﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;

namespace TracNghiemTinHocDaiCuong.DAO
{
    public class BaiHocDAO
    {
        public static BaiHoc TimTheoId(int idBaiHoc)
        {
            string query = string.Format("SELECT * FROM BaiHoc WHERE MaBH=@MaBH");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaBH", idBaiHoc)
            };
            IDataReader read = SqlHelper.ExecuteReader(query, parameter);
            BaiHoc BaiHoc = new BaiHoc();
            while (read.Read())
            {
                BaiHoc = DoiTuong(read);
            }
            return BaiHoc;
        }

        public static BaiHoc DoiTuong(IDataRecord read)
        {
            return new BaiHoc
            {
                MaBH = Methods.ConvertNumber(read["MaBH"].ToString()),
                TenBH = Methods.ConvertString(read["TenBH"].ToString()),
                MaCD = Methods.ConvertNumber(read["MaCD"].ToString()),
                NoiDung = Methods.ConvertString(read["NoiDung"].ToString()),
                MaBaiTiep = Methods.ConvertNumber(read["MaBaiTiep"].ToString())
            };
        }

        public static IList<BaiHoc> LayDanhSach()
        {
            IList<BaiHoc> danhSach = new List<BaiHoc>();
            string query = string.Format("SELECT * FROM BaiHoc");
            IDataReader read = SqlHelper.ExecuteReader(query);
            while(read.Read())
            {
                danhSach.Add(DoiTuong(read));
            }
            return danhSach;
        }

        public static bool Them(BaiHoc baiHoc)
        {
            string query = string.Format("INSERT INTO BaiHoc(TenBH, MaCD, NoiDung, MaBaiTiep) "+
            "VALUES(@TenBH, @MaCD, @NoiDung, @MaBaiTiep)");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@TenBH", baiHoc.TenBH),
                new SqlParameter("@MaCD", baiHoc.MaCD),
                new SqlParameter("@NoiDung", baiHoc.NoiDung)
            };
            if (baiHoc.MaBaiTiep != -1)
                parameter.Add(new SqlParameter("@MaBaiTiep", baiHoc.MaBaiTiep));
            else
                parameter.Add(new SqlParameter("@MaBaiTiep", DBNull.Value));
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Sua(BaiHoc baiHoc)
        {
            string query = string.Format("UPDATE BaiHoc SET TenBH=@TenBH, MaCD=@MaCD, NoiDung=@NoiDung "+
                ", MaBaiTiep=@MaBaiTiep WHERE MaBH=@MaBH");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@TenBH", baiHoc.TenBH),
                new SqlParameter("@MaCD", baiHoc.MaCD),
                new SqlParameter("@NoiDung", baiHoc.NoiDung),
                new SqlParameter("@MaBaiTiep", baiHoc.MaBaiTiep),
                new SqlParameter("@MaBH", baiHoc.MaBH)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Xoa(int idBaiHoc)
        {
            string query = string.Format("DELETE FROM BaiHoc WHERE MaBH=@MaBH");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaBH", idBaiHoc)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }
    }
}